package com.cwt.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.DateUtils;
import com.cwt.service.domain.ShopCar;
import com.cwt.service.domain.ShopCollect;
import com.cwt.service.service.ShopCollectService;
import com.cwt.service.mapper.ShopCollectMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
* @author Acer
* @description 针对表【tb_shop_collect(商品收藏表)】的数据库操作Service实现
* @createDate 2023-08-09 10:24:19
*/
@Service
public class ShopCollectServiceImpl extends ServiceImpl<ShopCollectMapper, ShopCollect>
    implements ShopCollectService{

    @Override
    public AjaxResult addCollect(ShopCollect collect) {
        QueryWrapper<ShopCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true, "user_id", collect.getUserId());
        queryWrapper.eq(true, "book_id", collect.getBookId());
        ShopCollect existCol = baseMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(existCol)) {
            collect.setCreateTime(DateUtils.formatFullDateTime(new Date()));
            return baseMapper.insert(collect) > 0
                    ? AjaxResult.success(collect.getBookName() + " 书籍添加收藏成功！")
                    : AjaxResult.error(collect.getBookName() + " 书籍添加收藏失败！");
        }

        // 更新时间
        existCol.setCreateTime(DateUtils.formatFullDateTime(new Date()));
        return baseMapper.updateById(existCol) > 0
                ? AjaxResult.success(existCol.getBookName() + " 书籍添加收藏成功！")
                : AjaxResult.error(existCol.getBookName() + " 书籍添加收藏失败！");
    }
}




